package health.lm.com.component_base.widget;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.lm.sjy.alertview.AlertView;
import com.lm.sjy.alertview.OnItemClickListener;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;

import health.lm.com.component_base.dialog.DefaultAlertDialog;
import health.lm.com.component_base.util.GoSettingPermission;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * 图片选择
 */
public class SelectAvatarAlertDialog implements OnItemClickListener {
    private Activity activity;
    private SelectAvatarUtil selectAvatarUtil;
    private SelectAvatarListener selectAvatarListener;
    public void setSelectAvatarListener(SelectAvatarListener selectAvatarListener) {
        this.selectAvatarListener = selectAvatarListener;
    }
    public interface  SelectAvatarListener{
        void selectAvatarSuccess(File outputFile, Uri outputUri);
    }
    public SelectAvatarAlertDialog(Activity activity) {
        this.activity = activity;
        selectAvatarUtil = new SelectAvatarUtil(activity, new SelectAvatarUtil.PhotoSelectListener() {
            @Override
            public void onFinish(File outputFile, Uri outputUri) {
                selectAvatarListener.selectAvatarSuccess(outputFile,outputUri);
            }
        },true);
    }


    @SuppressLint("CheckResult")
    @Override
    public void onItemClick(Object o, int position) {
        if (position == 0) {
            //拍照
            new RxPermissions(activity).requestEachCombined(Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Consumer<Permission>() {
                @Override
                public void accept(@NonNull Permission permission) throws Exception {
                    if (permission.granted) {
                        selectAvatarUtil.takePhoto();
                    } else if (permission.shouldShowRequestPermissionRationale){
                        //拒绝权限请求
                        ToastUtils.showShort("请开启相机和读写手机存储权限");
                    } else {
                        // 拒绝权限请求,并不再询问
                        // 可以提醒用户进入设置界面去设置权限
                        ToastUtils.showShort("已拒绝相机和读写手机存储权限，请到设置页面开启权限");
                        showAlertDialog("开启相机和读写手机存储权限");
                    }
                }
            });

        } else if (position == 1) {
            //选择照片
            new RxPermissions(activity).requestEachCombined(Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Consumer<Permission>() {
                @Override
                public void accept(@NonNull Permission permission) throws Exception {
                    if (permission.granted) {
                        selectAvatarUtil.selectPhoto();
                    } else if (permission.shouldShowRequestPermissionRationale){
                        //拒绝权限请求
                        ToastUtils.showShort("请开启读写手机存储权限");
                    } else {
                        // 拒绝权限请求,并不再询问
                        // 可以提醒用户进入设置界面去设置权限
                        ToastUtils.showShort("已拒绝读写手机存储权限，请到设置页面开启权限");
                        showAlertDialog("读写手机存储权限");
                    }
                }
            });

        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data){
        selectAvatarUtil.attachToActivityForResult(requestCode, resultCode, data);
    }
    public void show(){
        new AlertView(null, null, "取消", null,
                new String[]{"拍照", "从相册中选择"},
                activity, AlertView.Style.ActionSheet, this).show();
    }
    DefaultAlertDialog dialog;
    private void showAlertDialog(String msg) {

        dialog = new DefaultAlertDialog(activity).builder().setTitle("权限设置")
                .setMsg(msg).setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                }).setPositiveButton("去设置", new DefaultAlertDialog.OnClickListenerAlertDialog() {
                    @Override
                    public void onClick(View v, Dialog dialog) {
                        //去设置
                        dialog.dismiss();
                        GoSettingPermission.start(activity,false);
                    }
                });

        dialog.show();
    }
}
